﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;

namespace BookM
{
    public partial class AddPage : Form
    {
        public AddPage()
        {
            InitializeComponent();
        }

        #region 对象
        BookManagementBLL BMBLL = new BookManagementBLL();
        #endregion

        /// <summary>
        /// 新增图书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_AddBook_Click(object sender, EventArgs e)
        {
            //判断用户的值
            if (Judge())
            {
                //保存图书
                AddBook();
            }
            
        }

        /// <summary>
        /// 新加图书实现方法
        /// </summary>
        private void AddBook()
        {
            int CategoryType = int.Parse(this.cb_CategoryType.SelectedValue.ToString());
            BookInfo book = new Model.BookInfo(txb_Name.Text, CategoryType, txb_Author.Text, int.Parse(txb_Money.Text), txb_BookID.Text);
            string str = BMBLL.AddBook(book);
            if (str.Equals(EnumTools.GetDescription(ManageEnums.DOCUMENT.Add_SUCCEED)))
            {
                MessageBox.Show(str);
                this.Close();
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(str);
            }
        }

        /// <summary>
        /// 窗体加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddPage_Load(object sender, EventArgs e)
        {
            //绑定下拉框数据
            cb_CategoryType.DisplayMember = "CategoryName";
            cb_CategoryType.ValueMember = "ID";
            cb_CategoryType.DataSource = BMBLL.FindCategory();
        }

        /// <summary>
        /// 判断用户的值
        /// </summary>
        private bool Judge()
        {
            Regex reg = new Regex(@"^(\+)?\d+(\.\d+)?$");
            Regex str = new Regex("^[A-Za-z0-9]+$");
            if (txb_Name.Text == "")
            {
                MessageBox.Show("图书名称不能为空");
                return false;
            }
            else if (!str.Match(txb_BookID.Text).Success)
            {
                MessageBox.Show("请输入正确的图书编号");
                return false;
            }
            else if (this.cb_CategoryType.SelectedValue.ToString() == "")
            {
                MessageBox.Show("请选择图书分类");
                return false;
            }
            else if (txb_Author.Text == "")
            {
                MessageBox.Show("图书作者不能为空");
                return false;
            }
            else if (txb_Money.Text == "" )
            {
                MessageBox.Show("请输入价格");
                return false;
            }
            else if (!reg.Match(txb_Money.Text).Success)
            {
                MessageBox.Show("请输入正确的价格");
                return false;
            }
            else if (txb_BookID.Text == "")
            {
                MessageBox.Show("请输入图书id");
                return false;
            }
            return true;
        }
    }
}
